<template>
  <div class="editor-tools-bar">
    <span class="tool-bar-title">
      自定义设置
    </span>
    <a href="javaScript:;" @click="restore()">恢复默认</a>
    <a href="javaScript:;" @click="setData()" >保存</a>
  </div>
</template>

<script>
import { ElMessageBox } from 'element-plus';
import { AxiosActiveUser } from '@/api'
import router from '@/router';
import store from '@/store';

export default {
    setup(){
      // 发请求保存数据
        const setData = ()=>{
            ElMessageBox.confirm('确定修改吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                })
                .then(() => {
                    const userPositton = Array.from(store.state.components)
                    const dataitem = { //默认数据
                        "code": "",
                        "name": "",
                        "screen_num":'1',
                        "is_show":"1",
                        "style": {
                          "left": "0",
                          "right": "0",
                          "top": "0",
                          "bottom": "0",
                          "width": "0",
                          "height": "0"
                        }
                    }
                    let allData = []
                    userPositton.forEach(item=>{
                      let list = {...dataitem}
                      list.code = String(item.code)
                      list.name = String(item.name)
                      list.style = {...list.style,...item.position,...item.size}
                      list.style.height= String(list.style.height)
                      list.style.bottom= String(list.style.bottom)
                      list.style.left= String(list.style.left)
                      list.style.right= String(list.style.right)
                      list.style.top= String(list.style.top)
                      list.style.width= String(list.style.width)
                      allData.push(list)
                    })
                    console.log(userPositton);
                    console.log(allData);

                    if (allData) {
                      sessionStorage.setItem('echartsData',JSON.stringify(allData))
                      setTimeout(() => {
                        router.push('/')
                      }, 1000);
                    }
                   

                    // AxiosActiveUser.setData({data:allData}).then(response=>{
                    //   console.log(response);
                    //   setTimeout(() => {
                    //     router.push('/')
                    //   }, 1000);
                    // })

                })
                .catch(() => {})

        }
        // 重置 恢复默认
        const restore = ()=>{
          ElMessageBox.confirm('确定恢复默认吗?当前修改将会被覆盖!', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                })
                .then(() => {
                    store.commit('restoreDefault')
                }).catch(()=>{})
        }
    return{
        setData,
        restore
    }
    }
}
</script>

<style lang="scss" scoped>
.editor-tools-bar {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 190px;
  background: #fff;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-around;
  border-bottom: 1px solid #ddd;
  .tool-bar-title{
      font-size: 12px;
      color: black;
  }
  a{
    display: inline-block;
    padding: 0 10px;
    height: 18px;
    color: #333;
    font-size: 12px;
    text-align: center;
    line-height: 18px;
    border-radius: 10px;
    text-decoration: none;
    background-color: white;
    border: solid 1px #ddd;
    z-index: 99;
    &:hover{
        color: white;
        background-color: #8df;
        border-color: transparent;
    }
  }
}
.preview-code-style {
  width: 100%;
  height: 100%;
  max-height: 72vh;
  overflow-y: auto;
  font-family: Menlo, Monaco, Consolas, Courier, monospace;
}
</style>
